package com.wuhanins.insmytestdemo.domainService.provider;

import com.wuhanins.core.promise.JPromiser;
import com.wuhanins.core.proxy.ProviderContext;
import com.wuhanins.core.proxy.base.ProxyStatic;
import com.wuhanins.http.httpStateCheckHelper.IHttpStateCheck;

import org.json.JSONException;
import org.json.JSONObject;

import static com.wuhanins.http.volley.WebApiVolleyHelper.NOTIFY_ASTOKEN_EXPIRED;
import static com.wuhanins.http.volley.WebApiVolleyHelper.NOTIFY_ASTOKEN_UNLOGIN;
import static com.wuhanins.http.volley.WebApiVolleyHelper.PROMISS_FRESHTOKEN_STATUS;
import static com.wuhanins.http.volley.WebApiVolleyHelper.PROMISS_FRESHTOKEN_STATUS_NON;

public class V4_StateChecker implements IHttpStateCheck {
    @Override
    public boolean httpTokenCheck(String result, JPromiser.Deffered<String, String> d, JPromiser me, ProviderContext pContext) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            int state = 0;
            if (jsonObject.has("State")) {
                state = jsonObject.getInt("State");
            }
            String errMsg = "";
            if (jsonObject.has("Message")) {
                errMsg = jsonObject.getString("Message");
            }

            /* 生成测试token
            http://192.168.8.142/WuhanIns.Web.Shell/swagger/ui/index#!/GenerateToken/GenerateToken_Get
            密码Ins123!@#
             */

            //20191210 通过message判断token过期
//            if(errMsg.contains("DotNetOpenAuth.Messaging.ProtocolFaultResponseException")){
            if (state == 4001) {

                //40002 access token 过期
                //40003 refresh token 过期
                //region accessToken过期 启用刷新token插件
                //不需要重复刷新token
                if (PROMISS_FRESHTOKEN_STATUS_NON.equals(pContext.getObj(PROMISS_FRESHTOKEN_STATUS))) {
                    return true;
                }

                if (me.getRunTimes() < 2) {
                    d.notify(NOTIFY_ASTOKEN_EXPIRED, me);
                } else {
                    d.notify(ProxyStatic.PROVIDER_Q_STATE_DONE, 100);
                    d.notify(NOTIFY_ASTOKEN_UNLOGIN, me);
                    d.reject(errMsg);
                }
                return false;
                //endregion
            } else if (errMsg.equalsIgnoreCase("Token过期") || state == 4002) {
                if (PROMISS_FRESHTOKEN_STATUS_NON.equals(pContext.getObj(PROMISS_FRESHTOKEN_STATUS))) {
                    return true;
                }
                d.notify(ProxyStatic.PROVIDER_Q_STATE_DONE, 100);
                d.notify(NOTIFY_ASTOKEN_UNLOGIN, me);
                d.reject(errMsg);
                return false;
            }

            //获取access_token时Secret错误，或者access_token无效 User Not Login	用户未登录
            //需要重新登录
            if (state == 4000) {
                d.notify(ProxyStatic.PROVIDER_Q_STATE_DONE, 100);
                d.notify(NOTIFY_ASTOKEN_UNLOGIN, me);
                d.reject(errMsg);
                return false;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            d.reject(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean proxyServiceStateCheck(int state) {
        return state == 2000 || state == 0;
    }
}
